package com.gdth.bank.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.gdth.bank.R;
import com.gdth.bank.bean.BfjlCpxqItem;
import com.gdth.database.bean.TAppBfrjCpxq;

public class JtcyCpzlListAdapter extends BaseAdapter{
	private LayoutInflater mInflater;
	
	private Context context;
	
	private List<BfjlCpxqItem> mData;
	
	public JtcyCpzlListAdapter(Context context, List<BfjlCpxqItem> mData) {
	    this.mInflater = LayoutInflater.from(context);
	    this.context = context;
	    this.mData = mData;
    }
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.mData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return this.mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final BfjlCpxqItem model = mData.get(position);
		final int number = position;
		ViewHolder holder;
        //观察convertView随ListView滚动情况            
        if (convertView == null) {
        	convertView = mInflater.inflate(R.layout.jtcyxxb_cpxq_listview, null);
        	holder = new ViewHolder();
        	/*得到各个控件的对象*/                    
        	holder.txt_xm = (CheckBox) convertView.findViewById(R.id.jtcy_cpxq_zl_checkbox);
        	
        	holder.txt_xm.setOnCheckedChangeListener(new OnCheckedChangeListener(){

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					// TODO Auto-generated method stub
					
					if(isChecked){
						
						model.setCheck(true);
						
					} else{
						model.setCheck(false);}
					
				}
        		
        	});
        	convertView.setTag(holder);//绑定ViewHolder对象                   
    	}else{
            holder = (ViewHolder)convertView.getTag();//取出ViewHolder对象                  
        }
        if (holder != null) {
        	if (model.isCheck()){
        		holder.txt_xm.setChecked(true);
        	}
        	
        		
        	holder.txt_xm.setText(model.getValue());
        }
        return convertView;
	}
	
	public final class ViewHolder{
		public CheckBox txt_xm;
    }
	
	public List<BfjlCpxqItem> updateItrem(List<TAppBfrjCpxq> arg0){
		for (TAppBfrjCpxq mTAppBfrjCpxq : arg0){
			for (BfjlCpxqItem item : mData) {
				if (mTAppBfrjCpxq.getCpzl() == Integer.valueOf(item.getKey().toString())){
					item.setCpid(mTAppBfrjCpxq.getId());
					item.setCheck(true);
				}
			}
			
		}
		
		this.notifyDataSetChanged();
		return  mData;
	}
}
